﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.JumpToRiskCartography.CApJRCJumpToRiskCartographyApp
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.JumpToRiskCartography.Resources;
using EngineeringInternalExtension;
using System;
using System.Collections.ObjectModel;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;


namespace DnProcessSimulateCommands.JumpToRiskCartography;

internal class CApJRCJumpToRiskCartographyApp
{
  private TxSimulationSnapshotDataEx _simulationSnapshot;
  private bool _isStateModified;
  private bool _isDocumentUnloading;

  public bool IsInMode
  {
    get => TxCartographyEx.IsInJumpToCartographyMode();
    set
    {
      if (TxCartographyEx.IsInJumpToCartographyMode() == value)
        return;
      if (value)
        this.EnterMode();
      else
        this.ExitMode();
    }
  }

  private void EnterMode()
  {
    this.SaveState();
    this._isStateModified = false;
    this._isDocumentUnloading = false;
    TxApplication.ActiveSelection.Cleared += new TxSelection_ClearedEventHandler(this.ActiveSelection_Changed);
    TxApplication.ActiveSelection.ItemsAdded += new TxSelection_ItemsAddedEventHandler(this.ActiveSelection_Changed);
    TxApplication.ActiveSelection.ItemsRemoved += new TxSelection_ItemsRemovedEventHandler(this.ActiveSelection_Changed);
    TxApplication.ActiveSelection.ItemsSet += new TxSelection_ItemsSetEventHandler(this.ActiveSelection_Changed);
    TxApplication.ActiveDocument.Unloading += new TxDocument_UnloadingEventHandler(this.ActiveDocument_Unloading);
    TxCartographyEx.SetJumpToCartographyMode();
    new TxCommandManager().ExecuteCommandByCommandProgId("GraphicViewerModes.ModeJumpToRiskCartographyCmd.1", (object) null, false);
  }

  private void ExitMode()
  {
    TxApplication.ActiveDocument.Unloading -= new TxDocument_UnloadingEventHandler(this.ActiveDocument_Unloading);
    TxApplication.ActiveSelection.Cleared -= new TxSelection_ClearedEventHandler(this.ActiveSelection_Changed);
    TxApplication.ActiveSelection.ItemsAdded -= new TxSelection_ItemsAddedEventHandler(this.ActiveSelection_Changed);
    TxApplication.ActiveSelection.ItemsRemoved -= new TxSelection_ItemsRemovedEventHandler(this.ActiveSelection_Changed);
    TxApplication.ActiveSelection.ItemsSet -= new TxSelection_ItemsSetEventHandler(this.ActiveSelection_Changed);
    TxCartographyEx.UnsetJumpToCartographyMode();
    if (!this._isStateModified || this._isDocumentUnloading || TxDoNotShowAgainMessageBox.Show(CApJRCJumpToRiskCartographyResources.ResetMessage, CApJRCJumpToRiskCartographyResources.CommandName, "JUMP_TO_RISK_CARTOGRAPHY_RESET_CHECK", MessageBoxButtons.YesNo, DialogResult.No) != DialogResult.Yes)
      return;
    this.ResetState();
  }

  private void ActiveDocument_Unloading(object sender, TxDocument_UnloadingEventArgs args)
  {
    this._isDocumentUnloading = true;
    this.IsInMode = false;
  }

  private void SaveState()
  {
    try
    {
      this._simulationSnapshot = new TxPhysicalRootEx().CreateSimulationSnapshot();
    }
    catch
    {
      this._simulationSnapshot = (TxSimulationSnapshotDataEx) null;
    }
  }

  private void ResetState()
  {
    if (this._simulationSnapshot == null)
      return;
    TxApplication.ActiveDocument.SimulationPlayer.Rewind();
    try
    {
      new TxPhysicalRootEx().ApplySimulationSnapshot(this._simulationSnapshot);
      TxApplication.RefreshDisplay();
    }
    catch
    {
    }
  }

  private void JumpToSimulationTime(ITxRoboticOperation operation, double time)
  {
    TxSimulationPlayer simulationPlayer = TxApplication.ActiveDocument.SimulationPlayer;
    if (simulationPlayer.IsSimulationRunning())
      return;
    bool ignoreSignals = TxApplication.Options.Simulation.IgnoreSignals;
    TxSimulationPlayer.IgnoreSignals = true;
    bool stopOnCollision = TxApplication.Options.Collision.StopOnCollision;
    TxApplication.Options.Collision.StopOnCollision = false;
    simulationPlayer.AskUserForReset(false);
    simulationPlayer.IsAutomaticErrorOutputEnabled = false;
    simulationPlayer.DoOnlyUnscheduledReset(false);
    Cursor current = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    try
    {
      this._isStateModified = true;
      if (TxApplication.ActiveDocument.CurrentOperation != operation)
        TxApplication.ActiveDocument.CurrentOperation = (ITxOperation) operation;
      simulationPlayer.JumpSimulationToTime(time, false, (TxSimulationPlayerSource) 1);
      TxApplication.RefreshDisplay();
    }
    catch (Exception ex)
    {
    }
    finally
    {
      Cursor.Current = current;
      simulationPlayer.AskUserForReset(true);
      simulationPlayer.IsAutomaticErrorOutputEnabled = true;
      TxSimulationPlayer.IgnoreSignals = ignoreSignals;
      TxApplication.Options.Collision.StopOnCollision = stopOnCollision;
    }
  }

  private void ActiveSelection_Changed(object sender, EventArgs args)
  {
    TxSelection activeSelection = TxApplication.ActiveSelection;
    TxObjectList allItems = activeSelection.GetAllItems();
    if (((Collection<ITxObject>) allItems).Count != 1)
      return;
    TxCartography txCartography = ((Collection<ITxObject>) allItems)[0] as TxCartography;
    if (TxCartography.op_Equality(txCartography, (ITxObject) null))
      return;
    int manipulatorElementId = activeSelection.GetLastPickedManipulatorElementId();
    if (manipulatorElementId <= -1)
      return;
    TxCartographyElement element = txCartography.Elements[manipulatorElementId];
    this.JumpToSimulationTime(txCartography.Operation, element.Time);
  }
}
